let pageNum;
let pageInfo;
$(function () {
    let ids = window.location.search.substr(0);
    let sta = ids.split("=")[1];
    if (sta == null || sta == '' || sta == 'undefind') {
        showMovies()
    } else if (sta == 0) {
        showMovies(0)
    } else if (sta == 1) {

        showMovies(1)
        console.log(document.getElementsByClassName("selected"))
        document.getElementsByClassName("selected")[0].className = ''
        document.getElementsByClassName("subnav")[0].children[1].className = 'selected'
    }
})


function hot(obj) {
    document.getElementsByClassName("sort-control sort-radio-checked")[0].className = "sort-control sort-radio";
    obj.getElementsByClassName("sort-control sort-radio")[0].className = "sort-control sort-radio-checked";
    showMovies()
}

function ontype(obj, num) {
    document.getElementsByClassName("selected")[num].className = "";
    obj.className = "selected";
    showMovies()
}

$(function () {
    $("#search_btn").click(function () {
        showMovies()
    })

})

function toTypes(pageNum, str1) {

    let status = document.getElementsByClassName("selected")[0].innerText;
    let type = document.getElementsByClassName("selected")[1].innerText;
    let country = document.getElementsByClassName("selected")[2].innerText;
    let time = document.getElementsByClassName("selected")[3].innerText;
    let checked = document.getElementsByClassName("sort-control sort-radio-checked")[0].parentElement.children[1].innerText;
    let search = $("#search_input").val();
    let str;
    let str2;
    let str3;
    let str4;
    let str5;
    switch (type) {
        case '爱情':
            str = "love";
            break;
        case '喜剧':
            str = "comedy";
            break;
        case '动画':
            str = "animation";
            break;
        case '剧情':
            str = "story";
            break;
        case '恐怖':
            str = "horror";
            break;
        case '惊悚':
            str = "frightened";
            break;
        case '科幻':
            str = "science_fiction";
            break;
        case '动作':
            str = "action";
            break;
        case '悬疑':
            str = "suspense";
            break;
        case '犯罪':
            str = "crime";
            break;
        case '冒险':
            str = "adventure";
            break;
        case '战争':
            str = "warfare";
            break;
        case '奇幻':
            str = "strange";
            break;
        case '运动':
            str = "movement";
            break;
        case '家庭':
            str = "family";
            break;
        case '古装':
            str = "ancient_costume";
            break;
        case '武侠':
            str = "swordsman";
            break;
        case '西部':
            str = "west";
            break;
        case '历史':
            str = "history";
            break;
        case '传记':
            str = "special_note";
            break;
        case '歌舞':
            str = "song_dance";
            break;
        case '黑色电影':
            str = "film_noir";
            break;
        case '短片':
            str = "shorts";
            break;
        case '纪录片':
            str = "documentary";
            break;
        case '戏曲':
            str = "traditional";
            break;
        case '音乐':
            str = "music";
            break;
        case '灾难':
            str = "calamity";
            break;
        case '青春':
            str = " youth";
            break;
        case '儿童':
            str = "children";
            break;
        case '动漫':
            str = "anime";
            break;
        case '其他':
            str = "other";
            break;
    }
    if (str1 == null || str1 == '' || str1 == 'undefind') {
        switch (status) {
            case '正在热映':
                str1 = 0;
                break;
            case '即将上映':
                str1 = 1;
                break;
            case '经典影片':
                str1 = 2;
                break;
        }
    }

    switch (checked) {
        case '按热门排序':
            str2 = "grossed";
            break;
        case '按时间排序':
            str2 = "duration";
            break;
        case '按评价排序':
            str2 = "score";
            break;
    }
    switch (country) {
        case '中国大陆':
            str3 = "中国大陆";
            break;
        case '美国':
            str3 = "美国";
            break;
        case '韩国':
            str3 = "韩国";
            break;
        case '日本':
            str3 = "日本";
            break;
        case '中国香港':
            str3 = "中国香港";
            break;
        case '中国台湾':
            str3 = "中国台湾";
            break;
        case '泰国':
            str3 = "泰国";
            break;
        case '印度':
            str3 = "印度";
            break;
        case '法国':
            str3 = "法国";
            break;
        case '英国':
            str3 = "英国";
            break;
        case '俄罗斯':
            str3 = "俄罗斯";
            break;
        case '意大利':
            str3 = "意大利";
            break;
        case '西班牙':
            str3 = "西班牙";
            break;
        case '德国':
            str3 = "德国";
            break;
        case '波兰':
            str3 = "波兰";
            break;
        case '澳大利亚':
            str3 = "澳大利亚";
            break;
        case '伊朗':
            str3 = "伊朗";
            break;
        case '其他':
            str3 = "其他";
            break;

    }
    switch (time) {
        case '2029':
            str4 = "2029-01-01";
            str5 = "2030-01-01";
            break;
        case '2028':
            str4 = "2028-01-01";
            str5 = "2029-01-01";
            break;
        case '2027':
            str4 = "2027-01-01";
            str5 = "2028-01-01";
            break;
        case '2026':
            str4 = "2026-01-01";
            str5 = "2027-01-01";
            break;
        case '2025':
            str4 = "2025-01-01";
            str5 = "2026-01-01";
            break;
        case '2024':
            str4 = "2024-01-01";
            str5 = "2025-01-01";
            break;
        case '2023':
            str4 = "2023-01-01";
            str5 = "2024-01-01";
            break;
        case '2022':
            str4 = "2022-01-01";
            str5 = "2023-01-01";
            break;
        case '2021':
            str4 = "2021-01-01";
            str5 = "2022-01-01";
            break;
        case '2020':
            str4 = "2020-01-01";
            str5 = "2021-01-01";
            break;
        case '2019':
            str4 = "2019-01-01";
            str5 = "2020-01-01";
            break;
        case '2018':
            str4 = "2018-01-01";
            str5 = "2019-01-01";
            break;
        case '2017':
            str4 = "2017-01-01";
            str5 = "2018-01-01";
            break;
        case '2016':
            str4 = "2016-01-01";
            str5 = "2017-01-01";
            break;
        case '2015':
            str4 = "2015-01-01";
            str5 = "2016-01-01";
            break;
        case '2014':
            str4 = "2014-01-01";
            str5 = "2015-01-01";
            break;
        case '2013':
            str4 = "2013-01-01";
            str5 = "2014-01-01";
            break;
        case '2012':
            str4 = "2012-01-01";
            str5 = "2013-01-01";
            break;
        case '2011':
            str4 = "2011-01-01";
            str5 = "2012-01-01";
            break;
    }

    if (str4 == null || str4 == undefined) {
        str4 = "1950-01-01"
        str5 = "3000-01-01"
    }
    let jsonData = {
        status: str1,
        type: str,
        order: str2,
        country: str3,
        firstDate: str4,
        endDate: str5,
        search: search,
        pageNum: pageNum,
        pageSize: 12
    };
    let total;

    $.ajax({
        type: 'get',
        url: serverConfig.serverUrl + `/devs/movieIndex/countryAndStatus`,
        async: false,
        data: jsonData,
        // data: {type:"comedy",country:"中国",status:1,firstDate: "",endDate: "",pageNum: 1,pageSize: 100},
        success: function (result) {
            console.log(result)
            let str = '';
            let data = result.data;
            total = result.total;
            let height = result.height;
            for (let i = 0; i < data.length; i++) {
                let score = data[i].score + "";
                let img = data[i].posterImg;
                let title = data[i].title;
                let id = data[i].id;
                let flag = true;
                if (score == "" || score == null || score == 0) {
                    flag = false;
                } else {
                    scoreBig = score.split(".")[0];
                    scoreShort = score.split(".")[1];
                }
                str += `<div class="movie-item" id="del">
                        <div class="poster">
                            <a href="../html/moviedatas.html?id=${id}"><img src="../imgs/movie-img/${img}" alt="">
                            </a>
                        </div>`
                if (height[i] != null && height[i] != '') {
                    str += `<div class="name">${height[i]}</div>`
                }
                else
                    str += `<div class="name">${title}</div>`
                if (flag) {
                    str += `
                                    <div class="score">${score}</div>`

                } else {
                    str += `<div class="score">暂无评分</div>`
                }
                str += `</div>`

            }
            str += `</div>`;
            document.getElementById("movies").innerHTML = str;

        }
    })
    return total;
}

function showMovies(status) {
    let total = toTypes(1, status);
    layui.use(function () {
        var laypage = layui.laypage;
        laypage.render({
            elem: 'page',
            count: total, // 数据总数,
            limit: 12,//每页显示的条数
            // curr:current_page,//当前页
            jump: function (obj, first) {
                let pageNum = obj.curr;
                console.log(pageNum)
                if (!first) {
                    toTypes(pageNum, status)
                }
            }
        })
    })
}
